package com.mrjy.maintain.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mrjy.common.entity.PromoteDetail;
import com.mrjy.common.entity.UserInfo;
import com.mrjy.common.util.GlobalConstant;
import com.mrjy.maintain.bo.IPromoteDetailBo;
import com.mrjy.maintain.dto.PromoteDetailDTO;
import com.mrjy.maintain.util.RoleInfoEnum;
/**
 * 推广明细 controller
 * @author csshui
 *
 */
@Controller
@RequestMapping("/maintain/promoteDetail")
public class PromoteDetailController {
	
	@Resource
	private IPromoteDetailBo promoteDetailBo;
	
	/**
	 * 分页查询推广明细列表（后台数据查询）
	 * @param 
	 * @return
	 */
	@RequestMapping("/queryPromoteDetailPageForBack")
	@ResponseBody
	public Object queryPromoteDetailPageForBack(PromoteDetailDTO dto ,HttpSession session){
		return promoteDetailBo.queryPromoteDetailPageForBack(dto ,session);
	}
	
	/**
	 * 修改结算状态
	 * @param info  info.ids
	 * @return
	 */
	@RequestMapping("/updateSettleStatus")
	@ResponseBody
	public Object updateSettleStatus(PromoteDetail info,HttpSession session){
		return promoteDetailBo.txUpdateSettleStatus(info,session);
	}
	
	/**
	 * 根据userId查询其总收益和待结算，已结算等个人信息
	 * @param info  info.recommendId 推广者id info.costTypeId 付费名目类型id（0:365会员，1：课程包） info.settleStatus 结算状态
	 * @return
	 */
	@RequestMapping("/querySumPromoteDetailByUserId")
	@ResponseBody
	public Object querySumPromoteDetailByUserId(PromoteDetailDTO dto, HttpSession session){
		return promoteDetailBo.querySumPromoteDetailByUserId(dto,session);
	}
	
	/**
	 * 根据推广者id查询其推广明细
	 * @param info info.recommendId 推广者id info.costTypeId 付费名目类型id（0:365会员，1：课程包） info.settleStatus 结算状态
	 * @param info.page  info.rows  
	 * @return
	 */
	@RequestMapping("/queryPromoteDetailListByRecommendId")
	@ResponseBody
	public Object queryPromoteDetailListByRecommendId(PromoteDetailDTO dto, HttpSession session){
		return promoteDetailBo.queryPromoteDetailListByRecommendId(dto,session);
	}

   /**
	 * 通过分销商id查询其团队的总收益和待结算收益
	 * @param dto dto.recommendId 分销商userId
	 * @param info.page  info.rows
	 * @return
	 */
	@RequestMapping("/queryPromoterMoneyListByRecommendId")
	@ResponseBody
	public Object queryPromoterMoneyListByRecommendId(PromoteDetailDTO dto, HttpSession session){
		return promoteDetailBo.queryPromoterMoneyListByRecommendId(dto);
	}
	
	/**
	 * 通过分销商id和推广者id查询团队某成员的明细
	 * @param dto dto.distributorId(分销商id)  dto.recommendId(团队队员id) dto.settleStatus(结算状态，0：未结算；1：已结算)
	 * @param info.page  info.rows
	 * @return 
	 */
	@RequestMapping("/queryTeanMemberDetailListByDistributorIdAndUserId")
	@ResponseBody
	public Object queryTeanMemberDetailListByDistributorIdAndUserId(PromoteDetailDTO dto, HttpSession session){
		return promoteDetailBo.queryTeanMemberDetailListByDistributorIdAndUserId(dto);
	}
	
	/**
	 * 通过分销商id查询其自己和团队成员的明细 按付款时间降序
	 * @param dto dto.distributorId (分销商id) dto.recommendId(分销商自己的id) dto.settleStatus(结算状态，0：未结算；1：已结算)
	 * @param dto.page,dto.rows(分页参数)
	 * @return 
	 */
	@RequestMapping("/queryDistributorDetailListByDistributorId")
	@ResponseBody
	public Object queryDistributorDetailListByDistributorId(PromoteDetailDTO dto, HttpSession session){
		return promoteDetailBo.queryDistributorDetailListByDistributorId(dto);
	}
	
	/**
	 * 导出excel
	 * @param dto
	 * @param response
	 * @return
	 */
	@RequestMapping("/export")
	@ResponseBody
	public Object export(PromoteDetailDTO dto,HttpServletResponse response,HttpSession session){
		return promoteDetailBo.exportExl(dto,response,session);
	}
	
	/**
	 * 
	 * @author csshui
	 * @date 2018年01月22日
	 * @desc 推广明细 -财务结算 付款到微信用户零钱
	 * @param dto
	 * @param session
	 * @return
	 */
	@RequestMapping("/settleByIds")
	@ResponseBody
	public Object settleByIds(PromoteDetailDTO dto,HttpSession session){
		Map<String, Object> map = new HashMap<String, Object>();
		UserInfo info = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		if(null == info){
			map.put("err_code", -1);
			map.put("err_msg", "当前用户为登录");
			return map;
		}else{
			if(info.getRoleId().intValue() != RoleInfoEnum.FINANCE_ADMIN.getRoleId()){
				map.put("err_code", -2);
				map.put("err_msg", "当前用户不是财务管理员");
				return map;
			}
		}
		if(dto.getIds() == null){
			map.put("err_code", -3);
			map.put("err_msg", "推广记录ids为空");
			return map;
		}
		return promoteDetailBo.txSettleByIds(dto,map);
	}
	
	/**
	 * 1.6.8.1
	 * 查询我的一级推广/二级推广 列表
	 * promoteType  first-一级  second-二级
	 * @param dto
	 * @param session
	 * @return
	 */
	@RequestMapping("/queryMyPromoterList")
	@ResponseBody
	public Object queryMyPromoterList(PromoteDetailDTO dto,HttpSession session){
		return promoteDetailBo.queryMyPromoterList(dto,session);
	}
}
